import aiohttp
import asyncio
from fake_useragent import UserAgent
import pymongo

client = pymongo.MongoClient()
client.drop_database("bilibili")
db = client.get_database("bilibili")
collection = db.get_collection("fanju")
ua = UserAgent()

urls = [
	f'https://api.bilibili.com/pgc/season/index/result?st=1&order=3&season_version=-1&spoken_language_type=-1&area=-1&is_finish=-1&copyright=-1&season_status=-1&season_month=-1&year=-1&style_id=-1&sort=0&page={page}&season_type=1&pagesize=20&type=1'
	for page in range(1, 198)]


async def fetch(session, url):
	print(f"正在爬取{url}。。。")
	async with session.get(url, headers={"user-agent": ua.random}) as res:
		content = await res.json()
		items = content['data']['list']
		collection.insert_many(items)
		print(f"保存成功{url}。。。")



async def task():
	async with aiohttp.ClientSession() as session:
		while urls:
			url = urls.pop()
			await fetch(session, url)


async def main():
	await asyncio.gather(*[asyncio.create_task(task()) for i in range(50)])


asyncio.run(main())
client.close()
